﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using TarreOA.TarreOAModels;

namespace TarreOA.TarreOABLL
{
    public class NetDiskCatalogFolderAndFileService
    {
        /// <summary>
        /// 返回当前目录下的所有子目录和文件
        /// </summary>
        /// <param name="ndcP"></param>
        /// <returns></returns>
        public List<NetDiskCatalogFolderAndFile> findByThePath(string ndcP)
        {
            List<NetDiskCatalogFolderAndFile> ffList = null;
            if (Directory.Exists(ndcP))
            {
                ffList = new List<NetDiskCatalogFolderAndFile>();
                string[] dirNames = Directory.GetDirectories(ndcP);
                if (dirNames != null && dirNames.Length > 0)
                {
                    for (int i = 0; i < dirNames.Length; i++)
                    {
                        NetDiskCatalogFolderAndFile ndcfaf = new NetDiskCatalogFolderAndFile();
                        ndcfaf.FolderOrFileName = dirNames[i].Substring(dirNames[i].LastIndexOf("\\") + 1);
                        ndcfaf.FolderOrFile = "文件夹";
                        ndcfaf.FoderOrFileCreateTime = Directory.GetLastWriteTime(dirNames[i]).ToString();
                        ndcfaf.FolderOrFilePath = dirNames[i];
                        ndcfaf.FolderOrFileSize = "-";
                        ffList.Add(ndcfaf);
                    }
                }
                string[] fileNames = Directory.GetFiles(ndcP);
                if (fileNames != null && fileNames.Length > 0)
                {
                    for (int i = 0; i < fileNames.Length; i++)
                    {
                        NetDiskCatalogFolderAndFile ndcfaf = new NetDiskCatalogFolderAndFile();
                        ndcfaf.FolderOrFileName = fileNames[i].Substring(fileNames[i].LastIndexOf("\\") + 1);
                        ndcfaf.FolderOrFile = "文件";
                        ndcfaf.FoderOrFileCreateTime = File.GetLastWriteTime(fileNames[i]).ToString();
                        ndcfaf.FolderOrFilePath = fileNames[i];
                        ndcfaf.FolderOrFileSize = ((new FileInfo(fileNames[i])).Length / 1024 + 1).ToString() + "K";
                        ffList.Add(ndcfaf);
                    }
                }
            }
            return ffList;
        }

        /// <summary>
        /// 返回当前目录下的所有子目录和子目录中的所有目录
        /// </summary>
        /// <param name="ndcP"></param>
        /// <returns></returns>
        public List<string> findCurrentCatalogAllFolder(string ndcP)
        {
            List<string> dirList = null;
            DirectoryInfo dirInfo = new DirectoryInfo(ndcP);
            if (dirInfo.Exists)
            {
                dirList = new List<string>();
                foreach (DirectoryInfo dir in dirInfo.GetDirectories())
                {
                    StringBuilder sbu = new StringBuilder();
                    string path = dir.FullName;
                    string text = dir.FullName.Replace(ndcP, "");
                    sbu.AppendLine(path + "," + text);
                    dirList.Add(sbu.ToString());
                    AddSonFolder(dirList, dir, ndcP);
                }
            }
            return dirList;
        }

        /// <summary>
        /// 子目录递归查找
        /// </summary>
        /// <param name="dirLists"></param>
        /// <param name="sonDir"></param>
        protected void AddSonFolder(List<string> dirLists, DirectoryInfo sonDir, string ndcP)
        {
            if (sonDir.GetDirectories().Length > 0)
            {
                foreach (DirectoryInfo dir in sonDir.GetDirectories())
                {
                    StringBuilder sbu = new StringBuilder();
                    string path = dir.FullName;
                    string text = dir.FullName.Replace(ndcP, "");
                    sbu.AppendLine(path + "," + text);
                    dirLists.Add(sbu.ToString());
                    AddSonFolder(dirLists, dir, ndcP);
                }
            }
        }

        /// <summary>
        /// 在指定目录和所有子目录中查找指定文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public List<NetDiskCatalogFolderAndFile> findFilesAtThePath(string path, string fileName)
        {
            List<NetDiskCatalogFolderAndFile> list = null;
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            if (dirInfo.Exists)
            {
                list = new List<NetDiskCatalogFolderAndFile>();
                foreach (FileInfo fileInfo in dirInfo.GetFiles())
                {
                    if (fileInfo.Name.IndexOf(fileName) > -1)
                    {
                        NetDiskCatalogFolderAndFile ndcFaF = new NetDiskCatalogFolderAndFile();
                        ndcFaF.FolderOrFile = "文件";
                        ndcFaF.FoderOrFileCreateTime = fileInfo.LastWriteTime.ToString();
                        ndcFaF.FolderOrFileName = fileInfo.Name;
                        ndcFaF.FolderOrFilePath = fileInfo.FullName;
                        ndcFaF.FolderOrFileSize = (fileInfo.Length / 1024 + 1) + "K";
                        list.Add(ndcFaF);
                    }
                }
                if (dirInfo.GetDirectories().Length > 0)
                {
                    FindFileAsSonDirectory(dirInfo, fileName, list);
                }
            }
            return list;
        }

        /// <summary>
        /// 文件的递归查找
        /// </summary>
        /// <param name="dirInfo"></param>
        /// <param name="fileName"></param>
        /// <param name="fileList"></param>
        private void FindFileAsSonDirectory(DirectoryInfo dirInfo, string fileName, List<NetDiskCatalogFolderAndFile> fileList)
        {
            foreach (DirectoryInfo sDirInfo in dirInfo.GetDirectories())
            {
                foreach (FileInfo fileInfo in sDirInfo.GetFiles())
                {
                    if (fileInfo.Name.IndexOf(fileName) > -1)
                    {
                        NetDiskCatalogFolderAndFile ndcFaF = new NetDiskCatalogFolderAndFile();
                        ndcFaF.FolderOrFile = "文件";
                        ndcFaF.FoderOrFileCreateTime = fileInfo.LastWriteTime.ToString();
                        ndcFaF.FolderOrFileName = fileInfo.Name;
                        ndcFaF.FolderOrFilePath = fileInfo.FullName;
                        ndcFaF.FolderOrFileSize = (fileInfo.Length / 1024 + 1) + "K";
                        fileList.Add(ndcFaF);
                    }
                }
                if (sDirInfo.GetDirectories().Length > 0)
                {
                    FindFileAsSonDirectory(sDirInfo, fileName, fileList);
                }
            }
        }
    }
}
